﻿using AiXiu.Common;
using AiXiu.Model;
using Reservoir.DAL;
using Reservoir.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Reservoir.BLL
{
    public class UserBLL
    {
        ReservoirDbContent dbContent = new ReservoirDbContent();
        UserDAL userDAL = new UserDAL();
        /// <summary>
        /// 通过用户编号查找
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public TBUser FindByUserId(string userId)
        {
            return dbContent.TBUser.SingleOrDefault(m => m.Id == userId);
        }
        public TBUser get(string id)
        {
            return userDAL.get(id);
        }
        /// <summary>
        /// 通过用户名登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public OperResult<TBUser> LoginByName(string Name, string password)
        {
            //a. 参数非空验证
            if (string.IsNullOrEmpty(Name) || string.IsNullOrEmpty(password))
            {
                return OperResult<TBUser>.Failed("用户名或密码不能为空");
            }
            //b.通过用户名查询用户，不存在返回错误信息提示
            UserDAL userDAL = new UserDAL();
            TBUser user = userDAL.getUserByName(Name);
            if (user == null)
            {
                return OperResult<TBUser>.Failed("用户名不存在！");
            }
            //c. 验证用户密码，SHAEncryption类中方法
            SHAEncryption encryption = new SHAEncryption();
            if (encryption.SHA1Encrypt(password) == user.Password)
            {
                return OperResult<TBUser>.Failed("输入的密码有误！");
            }
            // 查找用户的具体信息
            UserBLL userBLL = new UserBLL();
            TBUser users = userBLL.get(user.Id);
            return OperResult<TBUser>.Succeed(users);
        }
    }
}
